var md_product_list=(function($,md_list){
    var app={
        listvalid:{
            rules: {
                price: {
                    required: true,
                    number:true
                },
                sprice: {
                    number:true
                },
                quantity: {
                    required: true,
                    number:true
                },
                stock: {
                    number:true
                }
            },
            messages: {
                price: {
                    required: "价格不能为空",
                    number: "请输入数字"
                },
                sprice: {
                    number: "请输入数字"
                },
                quantity: {
                    required: "数量不能为空",
                    number: "请输入数字"
                },
                stock: {
                    number: "请输入数字"
                }
            }
        },
        initEvent:function(){
            var _this=this;
            $("#saveData").on("click",function(){
                $("#list_form .error").hide();
                var flag=0;
                var attributes_info="{";
                $(".property").each(function(key,value){
                    var value=$(this).val();
                    if(value==''){
                        return true;
                    }
                    var id=$(this).attr("data-id");
                    attributes_info+='"'+id+'":"'+value+'"';
                    if(key<$(".property").size()-1){
                        attributes_info+=",";
                    }
                    flag=flag+1;
                });
                if(flag==1){
                    attributes_info= attributes_info.substring(0,attributes_info.length-1) ;
                }
                attributes_info+="}";
                if(flag==0){
                    alert("对不起，至少填写一个属性！")
                    return false;
                }
                if ($("#list_form").valid()) {
                    var data={'model_id':$("#model_id").val(),'model_pid':$("#model_pid").val(),'price':$("#price").val(),'sprice':$("#sprice").val(),'quantity':$("#quantity").val(),'stock':$("#stock").val(),'remark':$("#remark").val()};
                    data.attributes_info=attributes_info;
                    md_common.postAjax("/admin/productlist/saveInfo",data,function(result){
                        if(result.info=="success"){
                            if(result.id){
                                $("#model_id").val(result.id);
                            }
                            window.location.href= window.location.href;
                        }
                        else{
                            md_common.showMessage(result.info);
                        }
                    });
                }
            });

            $(".update_item").on("click",function(){
                $("#list_loading").show();
                $("#list_form .error").hide();
                var id=$(this).attr("data-id");
                md_common.postAjax(_this.action+"loadInfoById",{'id':id},function(result){
                    if(result.list&&result.list.length>0){
                        _this.loadForm(result.list[0]);
                        var attributes=result.list[0]['attributesValue'];
                        var attributesValue=JSON.parse(attributes);
                        $.each(attributesValue,function(key,value){
                            var id="property_"+key;
                            $("#"+id).val(value);
                        });
                        $("#list_loading").hide();
                        $("#list_modal").modal();
                    }
                });
            });
        }
    }
    return $.extend(true, md_list, app);
})($,md_list||{});
if(typeof md_product_list!=undefined){
    $(function(){
        md_product_list.init();
    });
}
